<?php

require_once 'PipesImpl/Route.php';
require_once 'PipesImpl/User/Credentials.php';
require_once 'PipesImpl/User/AccessControl/Abstract.php';

class Pipes_User_AccessControl_Array implements PipesImpl_User_AccessControl_Abstract
{
	private $_controls = array();
	public function __construct(array $controls = array()) {
		$this->_controls = $controls;
	}

	public function can(PipesImpl_User_Credentials $credentials, PipesImpl_Route $route) {
		if (isset($this->_controls[$route->controller][$route->action][$credentials->identity])) {
			return $this->_controls[$route->controller][$route->action][$credentials->identity]; 
		} elseif (isset($this->_controls[$route->controller][$route->action]['*'])) {
			return $this->_controls[$route->controller][$route->action]['*'];
		} elseif (isset($this->_controls[$route->controller]['*'][$credentials->identity])) {
			return $this->_controls[$route->controller]['*'][$credentials->identity];
		} elseif (isset($this->_controls[$route->controller]['*']['*'])) {
			return $this->_controls[$route->controller]['*']['*'];
		} elseif (isset($this->_controls['*'][$route->action][$credentials->identity])) {
			return $this->_controls['*'][$route->action][$credentials->identity];
		} elseif (isset($this->_controls['*'][$route->action]['*'])) {
			return $this->_controls['*'][$route->action]['*'];
		} elseif (isset($this->_controls['*']['*']['*'])) {
			return $this->_controls['*']['*']['*'];
		}


		return false;
	}
}
